#!/usr/bin/python2.4
#
# Copyright 2011 Google Inc. All Rights Reserved.

from google.appengine.dist import use_library
from git.util import git_context
from git.data import git_account
use_library('django', '1.2')
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

from db import user

import os
import urllib

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

class MainPage(webapp.RequestHandler):
    def get(self):
        template_values = {}
        email = self.request.get('email')
        session_manager = git_context.GitContext.GetSessionManager()
        assertion = session_manager.GetAssertion()
        if assertion and assertion.GetVerifiedEmail():
            template_values['is_federated'] = True
            template_values['first_name'] = assertion.GetFirstName()
            template_values['last_name'] = assertion.GetLastName()
            template_values['email'] = assertion.GetVerifiedEmail()
            template_values['nick_name'] = assertion.GetFirstName() + ' ' + assertion.GetLastName()
            template_values['photo_url'] = assertion.GetPhotoUrl()
        else:
            template_values['email'] = email
            template_values['is_federated'] = False
        path = os.path.join(os.path.dirname(__file__),
                            '../templates/signup.tpl')
        self.response.out.write(template.render(path, template_values))

    def post(self):
        email = self.request.get('email')
        password = self.request.get('password')
        if not password:
            account_type = 1
            account_type_str = git_account.GitAccount.FEDERATED
        else:
            account_type = 0
            account_type_str = git_account.GitAccount.LEGACY
            if not email or not password:
                self.redirect('/staticpages/error.html?msg=' + urllib.quote('Email or password can\'t be empty!'))
                return
        first_name = self.request.get('firstName')
        last_name = self.request.get('lastName')
        nick_name = self.request.get('nickName')
        photo_url = self.request.get('photoUrl')
        if user.HasUser(email):
            self.redirect('/staticpages/error.html?msg=' + urllib.quote('The email "%s" is already existed in our database!' % (email)))
            return
        user_profile = user.UserProfile(parent=user.UserKey(email))
        user_profile.email = email
        user_profile.password = password
        user_profile.first_name = first_name
        user_profile.last_name = last_name
        user_profile.nick_name = nick_name
        user_profile.account_type = account_type
        user_profile.put()
        
        account = git_account.GitAccount(email, account_type_str)
        account.SetLocalId(email)
        account.SetPhotoUrl(photo_url)
        account.SetDisplayName(first_name + ' ' + last_name)
        git_context.GitContext.GetSessionManager().SetSessionAccount(account)

        self.redirect('/')


application = webapp.WSGIApplication([('/signup.do', MainPage)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
